package com.filldream.fastboot.common.okutil.stock;

import com.filldream.fastboot.common.okutil.stocktest.StockOrder2;
import com.filldream.fastboot.common.util.Assert;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

public class StockOrderUtil {

    /**
     * 获取卖出收益
     * 以卖出为基准，计算卖出收益
     * @param buyOrder 买入订单
     * @return
     */
    public static String getOrderProfit(StockOrder2 buyOrder,StockOrder2 sellOrder){
        Assert.isBoolean( sellOrder.getIsBuy(),"以卖出为基准，计算卖出收益" );
        if( sellOrder.getCount() < 1 ){
            return "0";
        }
        TransactionBase.check(sellOrder.getCount());
        String buyCost = buyOrder.getBuyCost(buyOrder.getPrice(), buyOrder.getCount().toString());
        String sellPrice = TransactionBase.getProfit(sellOrder.getPrice(),sellOrder.getCount().toString());
        BigDecimal result = new BigDecimal(sellPrice).subtract(new BigDecimal(buyCost));
        return result.toString();
    }

    /**
     * 获取指定利润需要多少价位
     */
    public static String getBreakEvenPrice(StockOrder2 buyOrder,String targetProfit){
        Assert.isBoolean( !buyOrder.getIsBuy(),"以卖入为基准，计算达到指定收益股票需要涨到多少价位" );
        if( StringUtils.isBlank(targetProfit) ){  targetProfit = "0";  }
        //价格递增
        BigDecimal tempPrice = new BigDecimal(buyOrder.getPrice());
        StockOrder2 sellOrder = new StockOrder2();
        sellOrder.setPrice(tempPrice.toString());
        sellOrder.setCount(buyOrder.getCount());
        sellOrder.setIsBuy(false);

        String orderProfit = getOrderProfit(buyOrder,sellOrder);
        boolean isOk = new BigDecimal(orderProfit).compareTo( new BigDecimal(targetProfit) ) >= 0;

        //计算是否满足利润
        while ( !isOk ) {
            tempPrice = tempPrice.add( new BigDecimal("0.01") );
            sellOrder.setPrice(tempPrice.toString());
            BigDecimal tempProfit = new BigDecimal(getOrderProfit(buyOrder,sellOrder));
            isOk = tempProfit.compareTo( new BigDecimal(targetProfit) ) >= 0;
        }
        //tempPrice四舍五入保留两位小数
        return tempPrice.setScale(2, RoundingMode.HALF_UP).toString();
    }


    /**
     * 获取做T后的买入价格
     * 相当于获取做T后的price价格
     * @param stockOrderList
     * @return
     */
    public static String doT(List<StockOrder2> stockOrderList) {
        List<StockOrder2> buyOrderList = stockOrderList.stream().filter(t -> t.getIsBuy()).collect(Collectors.toList());
        BigDecimal buyOrderAmount = new BigDecimal("0");
        BigDecimal buyOrderBuyCommission = new BigDecimal("0");
        //计算平均买入价
        for (StockOrder2 item : buyOrderList) {
            String itemAmount = TransactionBase.getAmount(item.getPrice(), item.getCount().toString());
            String itemBuyCommission = TransactionBase.getBuyCommission(item.getPrice(), item.getCount().toString());
            buyOrderAmount = buyOrderAmount.add(new BigDecimal(itemAmount));
            buyOrderBuyCommission = buyOrderBuyCommission.add(new BigDecimal(itemBuyCommission));
        }
        BigDecimal result = buyOrderAmount.subtract( buyOrderBuyCommission.divide(new BigDecimal(buyOrderList.size()), 2, RoundingMode.HALF_UP) );
        return result.toString();
    }


}
